home *** CD-ROM | disk | FTP | other *** search
/ Champak 43 / Vol 43.iso / games / phit.swf / scripts / __Packages / CPhitPiece.as < prev    next >
Encoding:
Text File  |  2007-07-13  |  15.0 KB  |  480 lines

  1. class CPhitPiece extends CMovieClipFresh
  2. {
  3.    var _location;
  4.    var _myField;
  5.    var _minTileExtent;
  6.    var _dragPoint;
  7.    var onPress;
  8.    var onRelease;
  9.    var onReleaseOutside;
  10.    var _maxTileExtent;
  11.    var mc_borderHost;
  12.    var _nTiles = 0;
  13.    var _isLerping = false;
  14.    var _lastSteadyLocation = null;
  15.    var _priorLocationTile = null;
  16.    var _lastPushIndex = -1;
  17.    var _lastPushCheckIndex = -1;
  18.    var _lastPushCheckResult = undefined;
  19.    function CPhitPiece()
  20.    {
  21.       super();
  22.       this._location = new Vector2D(0,0);
  23.       this.SetInteractive(true);
  24.    }
  25.    function get _tileWidth()
  26.    {
  27.       return this._myField._tileWidth;
  28.    }
  29.    function get _myField()
  30.    {
  31.       var _loc2_ = CPlayingField(this._parent._parent._parent);
  32.       FreshDebug.Assert(_loc2_ != undefined,"field != undefined");
  33.       return _loc2_;
  34.    }
  35.    function get _tilePosition()
  36.    {
  37.       return this._minTileExtent.GetCopy();
  38.    }
  39.    function get _borderThickness()
  40.    {
  41.       return 3;
  42.    }
  43.    function StartLerping()
  44.    {
  45.       this._isLerping = true;
  46.       this._location._x = this._x;
  47.       this._location._y = this._y;
  48.    }
  49.    function onEnterFrame()
  50.    {
  51.       super.onEnterFrame();
  52.       if(this._isLerping)
  53.       {
  54.          var _loc15_ = 0.5;
  55.          var _loc9_ = this._minTileExtent.GetCopy();
  56.          if(this._dragPoint)
  57.          {
  58.             var _loc14_ = new Vector2D(this._parent._xmouse,this._parent._ymouse);
  59.             var _loc16_ = _loc14_.GetSubtract(this._dragPoint);
  60.             _loc9_.Set(_loc16_);
  61.             _loc9_.DivideScalar(this._tileWidth);
  62.             _loc9_._x = Math.round(_loc9_._x);
  63.             _loc9_._y = Math.round(_loc9_._y);
  64.             var _loc13_ = CPlayingField(this._myField);
  65.             _loc9_._x = MathUtil.Clamp(_loc9_._x,0,_loc13_._trayWidth - 1);
  66.             _loc9_._y = MathUtil.Clamp(_loc9_._y,_loc13_._minTilePos,_loc13_._trayHeight - 1);
  67.             var _loc11_ = _loc9_.GetSubtract(this._minTileExtent);
  68.             var _loc3_ = _loc11_.GetCopy();
  69.             _loc3_.SnapToMajorAxis();
  70.             _loc3_.Normalize();
  71.             if(_loc3_.MagnitudeSquared() > 0.0001)
  72.             {
  73.                var _loc12_ = 2;
  74.                while(true)
  75.                {
  76.                   _loc12_;
  77.                   if(!_loc12_--)
  78.                   {
  79.                      break;
  80.                   }
  81.                   var _loc5_ = this._minTileExtent.GetMultiplyScalar(this._tileWidth);
  82.                   var _loc4_ = _loc5_.GetSubtract(this._location);
  83.                   var _loc6_ = _loc4_.MagnitudeSafe();
  84.                   var _loc8_ = Math.abs(_loc4_.DotProduct(_loc3_));
  85.                   if(_loc6_ < 2 || _loc8_ > 0.01)
  86.                   {
  87.                      var _loc7_ = this.AttemptMoveByDeltaWithPush(_loc3_);
  88.                      if(_loc7_)
  89.                      {
  90.                         break;
  91.                      }
  92.                      _loc3_ = _loc11_.GetCopy();
  93.                      _loc3_.SnapToMinorAxis();
  94.                      _loc3_.Normalize();
  95.                   }
  96.                }
  97.             }
  98.          }
  99.          _loc9_ = this._minTileExtent.GetCopy();
  100.          _loc9_.MultiplyScalar(this._tileWidth);
  101.          this._location.Set(MathUtil.VectorLerp(_loc9_,this._location,_loc15_));
  102.          var _loc10_ = this._location.GetCopy();
  103.          _loc10_.DivideScalar(this._tileWidth);
  104.          _loc10_._x = Math.round(_loc10_._x);
  105.          _loc10_._y = Math.round(_loc10_._y);
  106.          if(this._priorLocationTile == null)
  107.          {
  108.             this._priorLocationTile = _loc10_.GetCopy();
  109.          }
  110.          if(Math.abs(_loc10_._x - this._priorLocationTile._x) > 0.1 || Math.abs(_loc10_._y - this._priorLocationTile._y) > 0.1)
  111.          {
  112.          }
  113.          this._x = this._location._x;
  114.          this._y = this._location._y;
  115.          this._priorLocationTile.Set(_loc10_);
  116.       }
  117.    }
  118.    function SetInteractive(isInteractive)
  119.    {
  120.       if(isInteractive)
  121.       {
  122.          this.onPress = this.OnMousePress;
  123.          this.onRelease = this.OnMouseRelease;
  124.          this.onReleaseOutside = this.OnMouseRelease;
  125.          this.useHandCursor = true;
  126.       }
  127.       else
  128.       {
  129.          this._dragPoint = null;
  130.          this.onPress = null;
  131.          this.onRelease = null;
  132.          this.onReleaseOutside = null;
  133.          this.useHandCursor = false;
  134.       }
  135.    }
  136.    function OnMousePress()
  137.    {
  138.       this._dragPoint = new Vector2D(this._xmouse,this._ymouse);
  139.       this.StartLerping();
  140.    }
  141.    function OnMouseRelease()
  142.    {
  143.       this._dragPoint = null;
  144.       if(!this._lastSteadyLocation.IsEqual(this._minTileExtent))
  145.       {
  146.          this._myField.RecordTravellingPieceMovement();
  147.       }
  148.    }
  149.    function RecordLastMove()
  150.    {
  151.       this._myField.RecordPieceMove(this,this._lastSteadyLocation.GetCopy(),this._minTileExtent.GetCopy());
  152.       this._lastSteadyLocation = this._minTileExtent.GetCopy();
  153.    }
  154.    function Create(r, g, b, arrTiles)
  155.    {
  156.       var _loc6_ = CPlayingField(this._myField);
  157.       this._minTileExtent = new Vector2D(100000,100000);
  158.       this._maxTileExtent = new Vector2D(-100000,-100000);
  159.       var _loc3_ = 0;
  160.       while(_loc3_ < arrTiles.length)
  161.       {
  162.          FreshDebug.Assert(arrTiles[_loc3_] != undefined,"arrTiles[ i ] != undefined");
  163.          this._minTileExtent._x = Math.min(this._minTileExtent._x,arrTiles[_loc3_]._x);
  164.          this._minTileExtent._y = Math.min(this._minTileExtent._y,arrTiles[_loc3_]._y);
  165.          this._maxTileExtent._x = Math.max(this._maxTileExtent._x,arrTiles[_loc3_]._x);
  166.          this._maxTileExtent._y = Math.max(this._maxTileExtent._y,arrTiles[_loc3_]._y);
  167.          _loc6_.SetTileOwner(arrTiles[_loc3_],this);
  168.          _loc3_ = _loc3_ + 1;
  169.       }
  170.       _loc3_ = 0;
  171.       while(_loc3_ < arrTiles.length)
  172.       {
  173.          var _loc4_ = new Vector2D((arrTiles[_loc3_]._x - this._minTileExtent._x) * this._tileWidth,(arrTiles[_loc3_]._y - this._minTileExtent._y) * this._tileWidth);
  174.          if(_loc4_ == undefined)
  175.          {
  176.             FreshDebug.Trace("Faulty tile location " + _loc3_);
  177.             FreshDebug.Trace(_loc4_.GetStringForm());
  178.             FreshDebug.Trace(this._minTileExtent.GetStringForm());
  179.             FreshDebug.Trace(this._tileWidth);
  180.          }
  181.          var _loc5_ = CPhitTile(this.attachMovie("Tile","tile" + _loc3_,this.getNextHighestDepth(),_loc4_));
  182.          _loc5_.Initialize(this,r,g,b);
  183.          _loc3_ = _loc3_ + 1;
  184.       }
  185.       this._nTiles = arrTiles.length;
  186.       this._x = this._minTileExtent._x * this._tileWidth;
  187.       this._y = this._minTileExtent._y * this._tileWidth;
  188.       this.RenderOutline(r << 16 | g << 8 | b);
  189.    }
  190.    function GetRandomCardinalDelta(upBias)
  191.    {
  192.       if(upBias != undefined)
  193.       {
  194.          if(upBias > _root.random.GetRandom())
  195.          {
  196.             return new Vector2D(0,-1);
  197.          }
  198.       }
  199.       var _loc2_ = new Vector2D(1,0);
  200.       if(_root.random.GetBoolean())
  201.       {
  202.          _loc2_._x = 0;
  203.          _loc2_._y = 1;
  204.       }
  205.       if(_root.random.GetBoolean())
  206.       {
  207.          _loc2_.Invert();
  208.       }
  209.       return _loc2_;
  210.    }
  211.    function SlideRandomly(minDesiredSteps, maxSteps, upBias)
  212.    {
  213.       if(minDesiredSteps == undefined)
  214.       {
  215.          minDesiredSteps = 1;
  216.       }
  217.       var _loc6_ = _root.random.GetIntInRange(minDesiredSteps,maxSteps);
  218.       var _loc5_ = this.GetRandomCardinalDelta(upBias);
  219.       var _loc3_ = 0;
  220.       while(_loc3_ < _loc6_)
  221.       {
  222.          var _loc4_ = this.AttemptMoveByDelta(_loc5_);
  223.          if(!_loc4_)
  224.          {
  225.             break;
  226.          }
  227.          _loc3_ = _loc3_ + 1;
  228.       }
  229.    }
  230.    function IsCompletelyOutsideOfTray()
  231.    {
  232.       var _loc4_ = CPlayingField(this._myField);
  233.       var _loc2_ = 0;
  234.       while(_loc2_ < this._nTiles)
  235.       {
  236.          var _loc3_ = this["tile" + _loc2_];
  237.          if(_loc4_.IsInTray(_loc3_._tilePosition))
  238.          {
  239.             return false;
  240.          }
  241.          _loc2_ = _loc2_ + 1;
  242.       }
  243.       return true;
  244.    }
  245.    function IsCompletelyInsideOfTray()
  246.    {
  247.       var _loc4_ = CPlayingField(this._myField);
  248.       var _loc2_ = 0;
  249.       while(_loc2_ < this._nTiles)
  250.       {
  251.          var _loc3_ = this["tile" + _loc2_];
  252.          if(!_loc4_.IsInTray(_loc3_._tilePosition))
  253.          {
  254.             return false;
  255.          }
  256.          _loc2_ = _loc2_ + 1;
  257.       }
  258.       return true;
  259.    }
  260.    function IsValidMoveByDelta(delta)
  261.    {
  262.       var _loc6_ = CPlayingField(this._myField);
  263.       var _loc2_ = 0;
  264.       while(_loc2_ < this._nTiles)
  265.       {
  266.          var _loc5_ = this["tile" + _loc2_];
  267.          var _loc4_ = _loc5_._tilePosition.GetAdd(delta);
  268.          if(!_loc6_.IsInBounds(_loc4_))
  269.          {
  270.             return false;
  271.          }
  272.          var _loc3_ = _loc6_.GetPieceAtTileLocation(_loc4_);
  273.          if(_loc3_ != null && _loc3_ != this)
  274.          {
  275.             return false;
  276.          }
  277.          _loc2_ = _loc2_ + 1;
  278.       }
  279.       return true;
  280.    }
  281.    function IsValidPosition(tilePos)
  282.    {
  283.       return this.IsValidMoveByDelta(tilePos.GetSubtract(this._minTileExtent));
  284.    }
  285.    function AttemptMoveByDelta(delta)
  286.    {
  287.       if(delta.MagnitudeSafe() < 0.00001)
  288.       {
  289.          return true;
  290.       }
  291.       if(this.IsValidMoveByDelta(delta))
  292.       {
  293.          this.MoveByDelta(delta);
  294.          return true;
  295.       }
  296.       return false;
  297.    }
  298.    function AttemptMoveByDeltaWithPush(delta)
  299.    {
  300.       if(delta.MagnitudeSafe() < 0.00001)
  301.       {
  302.          return true;
  303.       }
  304.       if(this.IsValidMoveByDeltaWithPush(delta,new Date().getTime() / 1000))
  305.       {
  306.          this.MoveByDeltaWithPush(delta,new Date().getTime() / 1000);
  307.          return true;
  308.       }
  309.       return false;
  310.    }
  311.    function IsValidMoveByDeltaWithPush(delta, recursionIndex)
  312.    {
  313.       if(recursionIndex == this._lastPushCheckIndex)
  314.       {
  315.          return this._lastPushCheckResult;
  316.       }
  317.       this._lastPushCheckResult = undefined;
  318.       this._lastPushCheckIndex = recursionIndex;
  319.       var _loc7_ = CPlayingField(this._myField);
  320.       var _loc3_ = 0;
  321.       while(_loc3_ < this._nTiles)
  322.       {
  323.          var _loc6_ = this["tile" + _loc3_];
  324.          var _loc5_ = _loc6_._tilePosition.GetAdd(delta);
  325.          if(!_loc7_.IsInBounds(_loc5_))
  326.          {
  327.             this._lastPushCheckResult = false;
  328.             return false;
  329.          }
  330.          var _loc2_ = _loc7_.GetPieceAtTileLocation(_loc5_);
  331.          if(_loc2_ != null && _loc2_ != this)
  332.          {
  333.             var _loc4_ = _loc2_.IsValidMoveByDeltaWithPush(delta,recursionIndex);
  334.             if(_loc4_ != undefined)
  335.             {
  336.                if(_loc4_ == false)
  337.                {
  338.                   this._lastPushCheckResult = false;
  339.                   return false;
  340.                }
  341.             }
  342.          }
  343.          _loc3_ = _loc3_ + 1;
  344.       }
  345.       this._lastPushCheckResult = true;
  346.       return true;
  347.    }
  348.    function MoveByDeltaWithPush(delta, recursionIndex)
  349.    {
  350.       if(recursionIndex == this._lastPushIndex)
  351.       {
  352.          return undefined;
  353.       }
  354.       this._lastPushIndex = recursionIndex;
  355.       var _loc4_ = CPlayingField(this._myField);
  356.       _loc4_.AddTravellingPiece(this);
  357.       var _loc3_ = 0;
  358.       while(_loc3_ < this._nTiles)
  359.       {
  360.          var _loc5_ = this["tile" + _loc3_];
  361.          var _loc6_ = _loc5_._tilePosition.GetAdd(delta);
  362.          var _loc2_ = _loc4_.GetPieceAtTileLocation(_loc6_);
  363.          if(_loc2_ != null && _loc2_ != this)
  364.          {
  365.             if(this._isLerping)
  366.             {
  367.                _loc2_.StartLerping();
  368.             }
  369.             _loc2_.MoveByDeltaWithPush(delta,recursionIndex);
  370.          }
  371.          if(_loc4_.GetPieceAtTileLocation(_loc5_._tilePosition) == this)
  372.          {
  373.             _loc4_.ClearTileOwner(_loc5_._tilePosition);
  374.          }
  375.          _loc3_ = _loc3_ + 1;
  376.       }
  377.       this._minTileExtent.Add(delta);
  378.       this._maxTileExtent.Add(delta);
  379.       if(!this._isLerping)
  380.       {
  381.          this._x = this._minTileExtent._x * this._tileWidth;
  382.          this._y = this._minTileExtent._y * this._tileWidth;
  383.       }
  384.       _loc3_ = 0;
  385.       while(_loc3_ < this._nTiles)
  386.       {
  387.          _loc5_ = this["tile" + _loc3_];
  388.          _loc4_.SetTileOwner(_loc5_._tilePosition,this);
  389.          _loc3_ = _loc3_ + 1;
  390.       }
  391.    }
  392.    function MoveByDelta(delta)
  393.    {
  394.       var _loc3_ = CPlayingField(this._myField);
  395.       var _loc2_ = 0;
  396.       while(_loc2_ < this._nTiles)
  397.       {
  398.          var _loc4_ = this["tile" + _loc2_];
  399.          if(_loc3_.GetPieceAtTileLocation(_loc4_._tilePosition) == this)
  400.          {
  401.             _loc3_.ClearTileOwner(_loc4_._tilePosition);
  402.          }
  403.          _loc2_ = _loc2_ + 1;
  404.       }
  405.       this._minTileExtent.Add(delta);
  406.       this._maxTileExtent.Add(delta);
  407.       if(!this._isLerping)
  408.       {
  409.          this._x = this._minTileExtent._x * this._tileWidth;
  410.          this._y = this._minTileExtent._y * this._tileWidth;
  411.       }
  412.       _loc2_ = 0;
  413.       while(_loc2_ < this._nTiles)
  414.       {
  415.          _loc4_ = this["tile" + _loc2_];
  416.          _loc3_.SetTileOwner(_loc4_._tilePosition,this);
  417.          _loc2_ = _loc2_ + 1;
  418.       }
  419.    }
  420.    function MoveToPosition(location, recordSteadyMove)
  421.    {
  422.       var _loc2_ = location.GetSubtract(this._minTileExtent);
  423.       this.MoveByDelta(_loc2_);
  424.       if(recordSteadyMove)
  425.       {
  426.          this._lastSteadyLocation = this._minTileExtent.GetCopy();
  427.       }
  428.    }
  429.    function RecordBaseSteadyPosition()
  430.    {
  431.       this._lastSteadyLocation = this._minTileExtent.GetCopy();
  432.    }
  433.    function RenderOutline(color)
  434.    {
  435.       var _loc8_ = CPlayingField(this._myField);
  436.       this.mc_borderHost = this.createEmptyMovieClip("mc_borderHost",this.getNextHighestDepth());
  437.       var _loc5_ = new Array();
  438.       _loc5_.push(new Vector2D(1,0));
  439.       _loc5_.push(new Vector2D(-1,0));
  440.       _loc5_.push(new Vector2D(0,1));
  441.       _loc5_.push(new Vector2D(0,-1));
  442.       var _loc6_ = 0;
  443.       while(_loc6_ < this._nTiles)
  444.       {
  445.          var _loc7_ = this["tile" + _loc6_];
  446.          var _loc4_ = _loc7_._tilePosition;
  447.          var _loc2_ = 0;
  448.          while(_loc2_ < _loc5_.length)
  449.          {
  450.             var _loc3_ = _loc5_[_loc2_];
  451.             if(_loc8_.GetPieceAtTileLocation(_loc4_.GetAdd(_loc3_)) != this)
  452.             {
  453.                this.RenderTileEdge(_loc4_.GetCopy(),_loc3_.GetCopy(),color);
  454.             }
  455.             _loc2_ = _loc2_ + 1;
  456.          }
  457.          _loc6_ = _loc6_ + 1;
  458.       }
  459.    }
  460.    function RenderTileEdge(tilePos, direction, color)
  461.    {
  462.       tilePos.Subtract(this._minTileExtent);
  463.       tilePos.MultiplyScalar(this._tileWidth);
  464.       tilePos.AddScalar(this._tileWidth * 0.5);
  465.       direction.MultiplyScalar(this._tileWidth * 0.5);
  466.       var _loc3_ = direction.GetCopy();
  467.       _loc3_.Transpose();
  468.       var _loc2_ = direction.GetCopy();
  469.       _loc2_.Transpose();
  470.       _loc2_.Invert();
  471.       _loc3_.Add(direction);
  472.       _loc2_.Add(direction);
  473.       _loc3_.Add(tilePos);
  474.       _loc2_.Add(tilePos);
  475.       this.mc_borderHost.lineStyle(this._borderThickness,color);
  476.       this.mc_borderHost.moveTo(_loc3_._x,_loc3_._y);
  477.       this.mc_borderHost.lineTo(_loc2_._x,_loc2_._y);
  478.    }
  479. }
  480.